using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class PageLabelBll
	{
		private Conn conn = null;
		public PageLabelBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public PageLabelBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAllPageLabel()
		{
			return this.conn.GetDataTable("Select * from [HL_PageLabel]  Order By ID Desc");
		}
		public bool AddPageLabel(string Name, string Type, string LabelTemplate)
		{
			string value = "";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Name", DbType.String, Name),
				this.conn.CreateParameter("@Type", DbType.String, Type),
				this.conn.CreateParameter("@Define", DbType.String, value),
				this.conn.CreateParameter("@Template", DbType.String, LabelTemplate),
				this.conn.CreateParameter("@UpdateTime", DbType.String, DateTime.Now)
			};
			return this.conn.Execute("Insert Into [HL_PageLabel]([Name], [Type], [Define], [Template], [UpdateTime]) Values(@Name, @Type, @Define, @Template, @UpdateTime)", parameters) == 1;
		}
		public bool UpdatePageLabel(int ID, string Name, string Type, string LabelTemplate)
		{
			DataTable pageLabelByID = this.GetPageLabelByID(ID);
			bool result;
			if (pageLabelByID.Rows.Count == 0)
			{
				result = false;
			}
			else
			{
				DataRow dataRow = pageLabelByID.Rows[0];
				if (Name == "")
				{
					Name = dataRow["Name"].ToString();
				}
				if (Type == "")
				{
					Type = dataRow["Type"].ToString();
				}
				if (LabelTemplate == "")
				{
					LabelTemplate = dataRow["Template"].ToString();
				}
				string value = "";
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@Name", DbType.String, Name),
					this.conn.CreateParameter("@Type", DbType.String, Type),
					this.conn.CreateParameter("@Define", DbType.String, value),
					this.conn.CreateParameter("@Template", DbType.String, LabelTemplate),
					this.conn.CreateParameter("@ID", DbType.Int32, ID)
				};
				result = (this.conn.Execute("Update [HL_PageLabel] Set [Name] = @Name, [Type] = @Type, [Define] = @Define, [Template] = @Template Where ([ID] = @ID)", parameters) == 1);
			}
			return result;
		}
		public DataTable GetPageLabelByID(int ID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ID", DbType.Int32, ID);
			return this.conn.GetDataTable("Select * from [HL_PageLabel] Where ([ID] = @ID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetPageLabelByName(string labelname)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Name", DbType.String, labelname);
			return this.conn.GetDataTable("Select * from [HL_PageLabel] Where ([Name] = @Name)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetPageLabelByType(string PageLabelType)
		{
			DataTable result;
			if (!string.IsNullOrEmpty(PageLabelType) && PageLabelType != "")
			{
				IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Type", DbType.String, PageLabelType);
				result = this.conn.GetDataTable("Select * from [HL_PageLabel] Where ([Type] = @Type)", new IDbDataParameter[]
				{
					dbDataParameter
				});
			}
			else
			{
				result = this.GetAllPageLabel();
			}
			return result;
		}
		public DataTable GetPageLabelType()
		{
			return this.conn.GetDataTable("Select Distinct([Type]) from [HL_PageLabel]");
		}
		public bool DeletePageLabel(int ID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ID", DbType.Int32, ID);
			return this.conn.Execute("DELETE From [HL_PageLabel] Where ([ID] = @ID)", new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public bool CopyPageLabelByID(int ID)
		{
			DataTable pageLabelByID = this.GetPageLabelByID(ID);
			bool result;
			if (pageLabelByID.Rows.Count == 1)
			{
				DataRow dataRow = pageLabelByID.Rows[0];
				result = this.AddPageLabel("复件 " + dataRow["Name"].ToString(), dataRow["Type"].ToString(), dataRow["Template"].ToString());
			}
			else
			{
				result = false;
			}
			return result;
		}
	}
}
